<?php
namespace app\zw_admin\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class ActivityController extends Controller
{
	//活动列表
    public function index()
    {
    	$activityList = db('activity')->where(['type'=>'1'])->order('aid desc')->select();
        $this->assign('activityList',$activityList);
        return $this->fetch();
    }

    /**
     * 添加活动
     */
    public function add()
    {
        if (Request::instance()->isPost()){
            $list = input('post.');
            $file = request()->file('img');
            // dump($file);
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    //获取图片路径
                    $list['activity_img'] =  'uploads/'.$info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }
            $list['time'] = date('Y-m-d H:i');
            $list['type'] = '1';
            //将数据添加至数据库
            if (db('activity')->insert($list) > 0) {
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
           
           
        }else{
            
            return $this->fetch('add');
        }
    }




    public function edit()
    {
        if (Request::instance()->isPost()){
            $list = input('post.');
            $file = request()->file('img');
            $aList = db('activity')->where(['aid'=>$list['aid']])->find();
            $aid = $list['aid'];
            // dump($file);
            // 移动到框架应用根目录/public/uploads/ 目录下
            if(!empty($file)){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    //获取图片路径
                    $list['activity_img'] =  'uploads/'.$info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }else{
                $list['activity_img'] = $aList['activity_img'];
            }
            $list['time'] = date('Y-m-d H:i');
            // unset($list['aid']);
            // dump($list);
            // exit;
            //将数据添加至数据库
            if (db('activity')->where(['aid'=>$aid])->update($list) > 0) {
                return $this->success('修改成功');
            }else{
                return $this->error('修改失败');
            }
           
           
        }else{
            // echo '111';
            $aid = input('param.aid');
            $aList = db('activity')->where(['aid'=>$aid])->select();
            // dump($aList);
            // exit;
            $this->assign('aList',$aList);
            return $this->fetch('edit');
        }

    }







    /**
     * 家居生活
     */
    public function home_index()
    {
        $activityList = db('activity')->where(['type'=>'2'])->order('aid desc')->select();
        $this->assign('activityList',$activityList);
        return $this->fetch();
    }





    /**
     * 添加家具生活
     */
    public function home_add()
    {
        if (Request::instance()->isPost()){
            $list = input('post.');
            $file = request()->file('img');
            // dump($file);
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    //获取图片路径
                    $list['activity_img'] =  'uploads/'.$info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }
            $list['time'] = date('Y-m-d H:i');
            $list['type'] = '2';
            //将数据添加至数据库
            if (db('activity')->insert($list) > 0) {
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
           
           
        }else{
            
            return $this->fetch();
        }
    }




}